package com.tinygray.queue;

import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.SynchronousQueue;
import java.util.concurrent.TimeUnit;

/**
 * @Author: tinygray
 * @Description: 公众号:Madison龙少，关注我你会越来越优秀。
 * @className: BlockQueueDemo
 * @create: 2021-05-09 15:04
 */
public class BlockQueueDemo {
    public static void main(String[] args) {

    }

    private static void SynchronousQueue() {
        SynchronousQueue<String> synchronousQueue = new SynchronousQueue<>();

        new Thread(()->{

            try {
                System.out.println(Thread.currentThread().getName() + "\t put" + 1);
                synchronousQueue.put("1");
                System.out.println(Thread.currentThread().getName() + "\t put" + 2);
                synchronousQueue.put("2");
                System.out.println(Thread.currentThread().getName() + "\t put" + 3);
                synchronousQueue.put("3");
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        },"AAA").start();

        new Thread(()->{
            try {
                //暂停线程5s
                Thread.currentThread().join(5000);
                System.out.println(Thread.currentThread().getName() + "\t" + synchronousQueue.take());
                //暂停线程5s
                Thread.currentThread().join(5000);
                System.out.println(Thread.currentThread().getName() + "\t" + synchronousQueue.take());
                //暂停线程5s
                Thread.currentThread().join(5000);
                System.out.println(Thread.currentThread().getName() + "\t" + synchronousQueue.take());
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        },"BBB").start();
    }

    private static void offer() throws InterruptedException {
        BlockingQueue<String> arrayBlockingQueue = new ArrayBlockingQueue<String>(3);
        //阻塞2秒  没有位置就走了
        System.out.println(arrayBlockingQueue.offer("a", 2L, TimeUnit.SECONDS));
        System.out.println(arrayBlockingQueue.offer("a", 2L, TimeUnit.SECONDS));
        System.out.println(arrayBlockingQueue.offer("a", 2L, TimeUnit.SECONDS));
        System.out.println(arrayBlockingQueue.offer("a", 2L, TimeUnit.SECONDS));
    }

    private static void putTake() throws InterruptedException {
        BlockingQueue<String> arrayBlockingQueue = new ArrayBlockingQueue<String>(3);
        //插入满了 阻塞
        arrayBlockingQueue.put("a");
        arrayBlockingQueue.put("b");
        arrayBlockingQueue.put("c");
        System.out.println("================================");
        //arrayBlockingQueue.put("d");
        //取元素  没有的话阻塞
        arrayBlockingQueue.take();
        arrayBlockingQueue.take();
        arrayBlockingQueue.take();
        arrayBlockingQueue.take();
    }

    private static void offerPeekPoll() {
        BlockingQueue<String> arrayBlockingQueue = new ArrayBlockingQueue<String>(3);
        //插入队列元素 插入满了 返回false
        System.out.println(arrayBlockingQueue.offer("a"));
        System.out.println(arrayBlockingQueue.offer("b"));
        System.out.println(arrayBlockingQueue.offer("c"));
        System.out.println(arrayBlockingQueue.offer("d"));
        //查看队列头元素
        System.out.println(arrayBlockingQueue.peek());
        //取出队列元素 取完了 返回false
        System.out.println(arrayBlockingQueue.poll());
        System.out.println(arrayBlockingQueue.poll());
        System.out.println(arrayBlockingQueue.poll());
        System.out.println(arrayBlockingQueue.poll());
    }

    private static void addRemoveElement() {
        BlockingQueue<String> arrayBlockingQueue = new ArrayBlockingQueue<String>(3);
        //添加队列元素  添加满了 报错 队列满了
        System.out.println(arrayBlockingQueue.add("a"));
        System.out.println(arrayBlockingQueue.add("b"));
        System.out.println(arrayBlockingQueue.add("c"));
        //查看队列头元素
        System.out.println(arrayBlockingQueue.element());
        //移除队列元素  移除没有了 报错
        System.out.println(arrayBlockingQueue.remove());
        System.out.println(arrayBlockingQueue.remove());
        System.out.println(arrayBlockingQueue.remove());
        System.out.println(arrayBlockingQueue.remove());
    }
}
